package com.mynews.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.mynews.bean.DailyListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/23.
 */

public class HomeFragmentViewpagerAdapter extends PagerAdapter{

    private DailyListBean dailyListBean;
    private List<View> views;
    private Context context;

    public HomeFragmentViewpagerAdapter(Context context,DailyListBean dailyListBean) {
        this.context = context;
        views = new ArrayList<>();
        this.dailyListBean = dailyListBean;
        dealData();

    }
    private void dealData() {
        List<DailyListBean.TopStoriesBean> top_stories = dailyListBean.getTop_stories();
        if(top_stories == null)return;
        for (int i = 0; i < top_stories.size(); i++) {
            ImageView view = new ImageView(context);
            ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            view.setLayoutParams(layoutParams);
            Glide.with(context).load(top_stories.get(i).getImage()).centerCrop().into(view);
            views.add(view);
        }
    }

    @Override
    public int getCount() {
        return views.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = views.get(position);
        container.addView(view);
        return view;
    }
}
